package com.rest.demo.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rest.demo.Service.Dysmtl;
import com.rest.demo.mapper.UsersMapper;
import com.rest.demo.repository.DysmtUser;
import com.rest.demo.util.Sm4Util;
import com.rest.demo.util.TimeDate;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Data
@RestController
@RequestMapping("/DysmtHealthSMT")//整个路由路径

public class GetUserHeathInfo_HC {
    @Resource
    public UsersMapper usersMapper;

    public String signature = "b1bc547c38e58853706d0ba56e8cd13";
    public DysmtUser RcUser = new DysmtUser();




    @RequestMapping("/test2")
    public String test() {
        RcUser.setCarId("CarId");
        RcUser.setUserName("UserName");
        RcUser.setNickname("1");
        RcUser.setPhone("17313368128");
        RcUser.setRegister((byte)0);
        boolean flag = usersMapper.add(RcUser) > 0;
        System.out.println(flag);
        return "hello";
    }


    //返回状态码 带token
    @RequestMapping("/GetHealthInfoRepoet")
    public JSONArray GetUserHeathInfoRepoet(String UserName, String CarId, String Token) {
        JSONArray states = new JSONArray();
        String decDatacontent = Sm4Util.dataDecrypt(Token, signature);//解密///
      System.out.println(decDatacontent);
        if (decDatacontent.equals("硬件") || decDatacontent.equals("中医院")|| decDatacontent.equals("二医院")) {
            Dysmtl DyHealth = new Dysmtl();
            JSONObject ReportInfo = DyHealth.GetHealthInfo(UserName, CarId);

            if(ReportInfo!=null){
            JSONArray Report = ReportInfo.getJSONObject("data").getJSONArray("healthyReport");
//            int rep = 0;
            // 使用传统for循环进行遍历
            for (int i = 0, size = Report.size(); i < size; i++) {
                String value = Report.getJSONObject(i).getString("healthyStaus");
//                rep = rep + Integer.valueOf(value);

                if (value.equals("00")) {
                    states.add("绿码");
                } else if (value.equals("01")) {
                    states.add("黄码");
                } else {
                    states.add("红码");
                }}
            this.SaveAllData(UserName,CarId,Token);
            }
//        System.out.println(ReportInfo.getJSONObject("data").getJSONObject("person").getString("desensitizeCardId"));
            return states;
        } else {
            JSONArray states2 = new JSONArray();
            states2.add("错误的密钥获取非法用户");
            return states2;

        }

    }



    public void SaveAllData(String UserName,String CarId,String Token) {
        String decDatacontent = Sm4Util.dataDecrypt(Token, signature);//解密///
        TimeDate hh= new TimeDate();
        RcUser.setUserName(UserName);
        RcUser.setCarId(CarId);
        RcUser.setPhone(decDatacontent);
        RcUser.setNickname( hh.NowTime());
        RcUser.setRegister((byte)1);
        RcUser.setCount(1);
        boolean flag = usersMapper.add(RcUser) > 0;
        System.out.println(flag);

    }




}
